package com.qf.bank.userprovider.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.bank.userapi.po.UserCardInfo;
import com.qf.bank.userapi.po.Users;
import com.qf.bank.userprovider.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 前端控制器
 *
 * @author UsersPo
 * @since 2019-12-04
 */
@RestController
public class UsersController {
  @Autowired private IUsersService usersService;
  //        用户分页
  @RequestMapping(value = "/", method = RequestMethod.GET)
  public IPage<Users> page(@RequestParam int current, @RequestParam int size) {
    IPage<Users> page = usersService.page(new Page<>(current, size));
    return page;
  }
  //    查询单个用户
  @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
  public Users getById(@PathVariable(value = "userId") int userId) {
    Users user = usersService.getById(userId);
    return user;
  }

  @RequestMapping(value = "/{userId}/cardInfo", method = RequestMethod.GET)
  public UserCardInfo selectCardInfoByUserId(@PathVariable int userId) {
    return usersService.selectCardInfoByUserId(userId);
  }

  /** 　* @描述: 根据电话号码查看用户信息 　* @时间: 2019/12/9 19:11 　 */
  @RequestMapping(value = "/list", method = RequestMethod.GET)
  public List<Users> getList(@RequestBody String userPhone) {
    System.out.println("--------------------");
    System.out.println(userPhone);
    List<Users> list =
        usersService.list(Wrappers.<Users>lambdaQuery().eq(Users::getUserPhone, userPhone));
    return list;
  }
  /** 　* @描述: 用户登录验证 　* @时间: 2019/12/5 13:30 　 */
  @RequestMapping(value = "/login", method = RequestMethod.POST)
  public Users getSelectLogin(@RequestBody Users users) {
    // mysqlMapper.getAll(Wrappers.<MysqlData>lambdaQuery().eq(MysqlData::getGroup, 1));
    Users user =
        usersService.getOne(
            Wrappers.<Users>lambdaQuery()
                .eq(Users::getUserPhone, users.getUserPhone())
                .eq(Users::getUserPassword, users.getUserPassword()));
    return user;
  }

  /** 　* @描述: 添加用户 　* @时间: 2019/12/5 13:32 　 */
  @RequestMapping(value = "/", method = RequestMethod.POST)
  public Boolean setInsert(@RequestBody Users users) {
    boolean result = usersService.save(users);
    return result;
  }

  /** 　* @描述: 根据用户id删除用户 　* @时间: 2019/12/5 13:50 　 */
  @RequestMapping(value = "/{userId}", method = RequestMethod.DELETE)
  public Boolean getDeleteId(@PathVariable(value = "userId") int userId) {
    Boolean result = usersService.removeById(userId);
    return result;
  }

  /** 　* @描述: 删除多个用户 　* @时间: 2019/12/5 13:53 　 */
  @RequestMapping(value = "/", method = RequestMethod.DELETE)
  public Boolean getDelete(@RequestBody List<Integer> list) {
    boolean result = usersService.removeByIds(list);
    return result;
  }

  /** 　* @描述: 根据用户id修改用户信息(修改部分信息) 　* @时间: 2019/12/5 13:51 　 */
  @RequestMapping(value = "/", method = RequestMethod.PATCH)
  public Boolean getChangeId(@RequestBody Users users) {
    System.out.println("************");
    System.out.println(users);
    boolean result = usersService.updateById(users);
    return result;
  }
}
